package com.yedean.controller.shop;

import com.yedean.constants.SystemCode;
import com.yedean.response.vo.PageVo;
import com.yedean.shop.dto.StoreDto;
import com.yedean.shop.entity.Store;
import com.yedean.shop.service.IStoreService;
import com.yedean.utils.Result;
import com.yedean.vo.shop.AddStoreVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * StoreController
 *
 * @author yedean
 * @date 2022/9/19
 */
@Slf4j
@Api(tags = { "门店信息" })
@RestController
@RequestMapping("/enterpise/store")
public class StoreController {
    @Reference(version = "1.0.0", check=false)
    private IStoreService storeService;

    /**
     * 分页数据
     */
    @ApiOperation(value = "分页查询所有门店")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "pageSize", value = "分大小", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "门店名称", required = false, dataType = "String") })
    @GetMapping(value = "/pageList/{page}/{pageSize}")
    public PageVo<Store> findListByPage(@PathVariable int page,
                                        @PathVariable int pageSize,
                                        @RequestParam(value = "name",required = false) String name) {
        return new PageVo<>(storeService.queryPageByName(page, pageSize, name));
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询数据")
    @ApiImplicitParam(paramType = "query", name = "id", value = "主键", required = true, dataType = "String")
    @GetMapping(value = "/{id}")
    public Store getById(@PathVariable String id) {
        return storeService.getById(id);
    }

    /**
     * 新增门店
     */
    @ApiOperation(value = "新增数据")
    @PostMapping(value = "/add")
    public boolean add(@RequestBody AddStoreVO storeVO) {
        Store store = new Store();
        BeanUtils.copyProperties(storeVO, store);
        return storeService.save(store);
    }


    /**
     * 门店停用
     */
    @ApiOperation(value = "门店停用")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键", required = true, dataType = "String")
    @PutMapping(value = "/disabled/{id}")
    public boolean disabled(@PathVariable String id) {

        Store store = storeService.getById(id);
        store.setStatus(SystemCode.FORBIDDEN);
        
        return storeService.updateById(store);
    }


    @ApiOperation(value = "获取当前商户管理的门店信息")
    @GetMapping(value = "/listManagerStores")
    public List<StoreDto> getStoreListByManagerId(){
        return storeService.getStoresByManagerId();
    }



    @ApiOperation(value = "门店切换")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "storeId", value = "门店Id", dataType = "String")})
    @GetMapping(value = "/switchStore/{storeId}")
    public Result switchStore(@PathVariable("storeId") String storeId){
        return storeService.switchStore(storeId);
    }




}
